package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Util 
{
	static public boolean isNumericInt(String str) {
		return str.matches("[0-9]+");
	}
	
	static public Calendar getDataAtual()
	{
		Calendar dataAtual = new GregorianCalendar();
		dataAtual.setTime( new Date() );
		return dataAtual;
	}
	
	static public Calendar toCalendar( String dataDDMMAAAA ) throws ParseException
	{
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Calendar data = Calendar.getInstance();
		data.setTime( dateFormat.parse( dataDDMMAAAA ) );

		return data;
	}
	
	static public String toString( Calendar dataCalend )
	{
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		String a = dateFormat.format( dataCalend.getTime() );
		return dateFormat.format( dataCalend.getTime() );
	}
	
	static public int getDiferencaEmAnos( Calendar data1, Calendar data2 )
	{
		int diferenca = data2.get(Calendar.YEAR) - data1.get(Calendar.YEAR);
		
		if (!completouUltimoAnoDiferenca( data1, data2 ))
			diferenca--;
		
		return diferenca;
	}
	
	static private boolean completouUltimoAnoDiferenca( Calendar data1, Calendar data2 )
	{
		boolean mesEhPosterior = ( data2.get(Calendar.MONTH) > data1.get(Calendar.MONTH) );
		boolean diaDoMesEhIgualOuPosterior = ( data2.get(Calendar.MONTH) == data1.get(Calendar.MONTH) &&
											   data2.get(Calendar.DAY_OF_MONTH) >= data1.get(Calendar.DAY_OF_MONTH));
		
		return(	mesEhPosterior || diaDoMesEhIgualOuPosterior );
	}
}
